//
//  ZYModifyPhoneEmailVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/31.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYModifyPhoneEmailVC.h"
#import "ZYViewUtils.h"
#import "ZYImageUtils.h"
#import "JKCountDownButton.h"
#import "ZYModifyUserInfoViewModel.h"
#import "ZYRegisterViewModel.h"
#import "ZYUser.h"

#define IDENTIFY_KEY_EMAIL @"identify_key_email"
#define IDENTIFY_KEY_PHONE @"identify_key_phone"

@interface ZYModifyPhoneEmailVC ()
@property(nonatomic,strong) UITextField *textField;
@property(nonatomic,strong) UITextField *codeField;
@property(nonatomic,strong) UIButton *confirmBtn;

/**
 修改用户信息
 */
@property(nonatomic,strong) ZYModifyUserInfoViewModel *modifyViewModel;

/**
 获取验证码
 */
@property(nonatomic,strong) ZYRegisterViewModel *codeViewModel;
@end

@implementation ZYModifyPhoneEmailVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(_type == ZYModifyTypePhone){
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_PHONE];
    }else{
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_EMAIL];
    }
}

#pragma mark - init
-(void)setup{
    [self setupTap];//点击关闭键盘
    [self setupView];
}

-(void)setupView{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    CGFloat padding = 15;
    //邮箱或手机号
    ZYUser *user = [ZYConfig userInfo];
    if(_type == ZYModifyTypePhone){
        _textField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_PHONE_LABEL] placeholder:[language getStringByKey:LC_ME_MAIN_MODIFY_PHONE_PLACEHOLDER]];
        _textField.text = user.phone;
    }else{
        _textField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_EMAIL_LABEL] placeholder:[language getStringByKey:LC_ME_MAIN_MODIFY_EMAIL_PLACEHOLDER]];
        _textField.text = user.email;
    }
    [self.view addSubview:_textField];
    _textField.top = self.navigationView.bottom;
    _textField.centerX = self.view.centerX;
    
    _codeField = [ZYViewUtils setupTextFieldWithLeftTitle:[language getStringByKey:@""] placeholder:[language getStringByKey:LC_ME_MAIN_MODIFY_CODE_PLACEHOLDER]];
    [self.view addSubview:_codeField];
    _codeField.top = _textField.bottom + padding;
    _codeField.centerX = self.view.centerX;
    [self setLeftView:_codeField];//左边空白
    _codeField.rightViewMode = UITextFieldViewModeAlways;
    _codeField.rightView = [self setupCodeButton];
    
    _confirmBtn = [ZYViewUtils setupConfirmBtn];
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:[language getStringByKey:LC_ME_MAIN_MODIFY_BUTTON_TITLE] forState:UIControlStateNormal];
    _confirmBtn.centerX = self.view.centerX;
    _confirmBtn.top = _codeField.bottom + 25;
    [_confirmBtn addTarget:self action:@selector(confirmModify:) forControlEvents:UIControlEventTouchUpInside];
}


/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}


/**
 初始化获取验证码按钮
 */
-(JKCountDownButton *)setupCodeButton{
    JKCountDownButton *codeBtn = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    codeBtn.frame =CGRectMake(0, 0, ZYSCREEN_W * 0.27, 45);
//    codeBtn.backgroundColor = kMainColor;
    [codeBtn setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:codeBtn.bounds] forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [codeBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [codeBtn setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_CODE_BTN_TITLE] forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(doGetCode:) forControlEvents:UIControlEventTouchUpInside];
    codeBtn.layer.cornerRadius = 3;
    codeBtn.layer.masksToBounds = YES;
    [codeBtn countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [countDownButton setBackgroundImage:[ZYImageUtils imageWithColor:RGBA(191, 191, 191, 1) rect:codeBtn.bounds] forState:UIControlStateNormal];
        NSString *title = [NSString stringWithFormat:@"%zds",second];
        return title;
    }];
    [codeBtn countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [countDownButton setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:codeBtn.bounds] forState:UIControlStateNormal];
        return [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_CODE_BTN_TITLE];
    }];
    return codeBtn;
}

#pragma mark -Event
//修改手机号或邮箱
-(void)confirmModify:(id)button{
    [self dismissKeyboard];
    self.modifyViewModel.code = _codeField.text;
    if(_type == ZYModifyTypePhone){
        self.modifyViewModel.phone = _textField.text;
        @weakify(self);
        [[self.modifyViewModel.phoneModifyCommand execute:nil] subscribeNext:^(id x) {
            @strongify(self);
            [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_MODIFY_PHONE_SUCCESS]];
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(NSError *error) {
            [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
        }];
        //监听状态
        [[self.modifyViewModel.phoneModifyCommand.executing skip:1] subscribeNext:^(id x) {
            BOOL executing = [x boolValue];
            ((UIButton *)button).enabled = !executing;
            [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
        }];
    }else{
        self.modifyViewModel.email = _textField.text;
        @weakify(self);
        [[self.modifyViewModel.emailModifyCommand execute:nil] subscribeNext:^(id x) {
            @strongify(self);
            [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_MODIFY_EMAIL_SUCCESS]];
            [self.navigationController popViewControllerAnimated:YES];
        } error:^(NSError *error) {
            [ZYBasicHelper handleErrorMessaeWithError:error message:[NSErrorHelper handleErrorMessage:error] controller:self];
        }];
        //监听状态
        [[self.modifyViewModel.emailModifyCommand.executing skip:1] subscribeNext:^(id x) {
            BOOL executing = [x boolValue];
            ((UIButton *)button).enabled = !executing;
            [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
        }];
    }
}

//获取验证码
-(void)doGetCode:(JKCountDownButton *)button{
    [self dismissKeyboard];
    NSString *phone = _textField.text;
    self.codeViewModel.phone = phone;
    if(_type == ZYRecoverPasswordTypeEmail){
        self.codeViewModel.modifyType = ZYModifyTypeEmail;
    }else{
        self.codeViewModel.modifyType = ZYModifyTypePhone;
    }
    [[self.codeViewModel.codeCommand execute:nil] subscribeNext:^(id x) {
        button.enabled = NO;
        //button type要 设置成custom 否则会闪动
        [button startCountDownWithSecond:60];
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_GET_CODE_SUCCESS]];
    } error:^(NSError *error) {
        [ZYBasicHelper handleErrorMessaeWithError:error message:[NSErrorHelper handleErrorMessage:error] controller:self];
    }];
    //监听状态
    [[self.codeViewModel.codeCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        button.enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}

#pragma mark - Getter
-(ZYRegisterViewModel *)codeViewModel{
    if(!_codeViewModel){
        _codeViewModel = [[ZYRegisterViewModel alloc] init];
    }
    return _codeViewModel;
}

-(ZYModifyUserInfoViewModel *)modifyViewModel{
    if(!_modifyViewModel){
        _modifyViewModel = [[ZYModifyUserInfoViewModel alloc] init];
    }
    return _modifyViewModel;
}

@end
